// 用户管理相关 API
import request from '@/utils/request';

import { PageResult } from '@/types/common';

// 用户创建请求参数
export interface UserCreateRequest {
  affiliatedInstitution: string;
  area: string;
  identityType: string;
  name: string;
  phoneNumber: string;
  position: string;
  researchDirection: string;
  status: number;
  unit: string;
}

// 用户更新请求参数
export interface UserUpdateRequest {
  id: number;
  affiliatedInstitution: string;
  area: string;
  identityType: string;
  name: string;
  position: string;
  researchDirection: string;
  unit: string;
  updateTime: string;
}

// 用户分页查询参数
export interface UserPageParams {
  current: number;
  size: number;
  ascs?: string;
  descs?: string;
  numberOrName?: string;
  roleType?: number;
  searchKey?: string;
  status?: number;
  teamName?: number;
}

// 用户详情响应
export interface UserDetailResponse {
  id: number;
  affiliatedInstitution: string;
  area: string;
  avatar: string;
  createTime: string;
  identityAuthenticationId: number;
  identityType: string;
  isDeleted: number;
  name: string;
  phoneNumber: string;
  position: string;
  researchDirection: string;
  roleId: number;
  shippingAddressId: number;
  status: number;
  teamId: number;
  unit: string;
  updateTime: string;
}

// 用户列表项响应
export interface UserListResponse {
  id: number;
  affiliatedInstitution: string;
  area: string;
  avatar: string;
  createTime: string;
  identityAuthenticationId: number;
  identityType: string;
  isDeleted: number;
  name: string;
  phoneNumber: string;
  position: string;
  researchDirection: string;
  roleId: number;
  roleType: number;
  shippingAddressId: number;
  status: number;
  teamId: number;
  teamName: string;
  unit: string;
  updateTime: string;
}

export const userApi = {
  // 创建用户
  createUser(data: UserCreateRequest): Promise<boolean> {
    return request.post('/admin/user/create', data);
  },

  // 获取用户详情
  getUserById(userId: number): Promise<UserDetailResponse> {
    return request.post('/admin/user/getInfoById', null, {
      params: { userId }
    });
  },

  // 获取用户分页列表
  getUserPageList(params: UserPageParams): Promise<PageResult<UserListResponse>> {
    return request.post('/admin/user/getPageList', params);
  },

  // 更新用户
  updateUser(data: UserUpdateRequest): Promise<boolean> {
    return request.post('/admin/user/update', data);
  },

  // ============ 原有接口保持兼容 ============

  // 获取用户列表 (旧接口)
  getUsers: (params: any): Promise<any> =>
    request.get('/users', { params }),

  // 获取用户详情 (旧接口)
  getUser: (id: string): Promise<any> =>
    request.get(`/users/${id}`),

  // 删除用户
  deleteUser: (id: string): Promise<any> =>
    request.delete(`/users/${id}`),

  // 批量删除用户
  batchDeleteUsers: (ids: string[]): Promise<any> =>
    request.post('/users/batch-delete', { ids }),

  // 重置用户密码
  resetPassword: (id: string): Promise<any> =>
    request.post(`/users/${id}/reset-password`),
};

// 工具函数：将分页接口转换为列表接口
export const getUserList = async (searchKey?: string): Promise<UserListResponse[]> => {
  try {
    const params: UserPageParams = {
      current: 1,
      size: 9999, // 获取所有数据
      searchKey,
      status: 1, // 只获取正常状态的用户
    };

    const response = await userApi.getUserPageList(params);
    return response.records;
  } catch (error) {
    console.error('获取用户列表失败:', error);
    return [];
  }
};